<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../../../static/css/login.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" type="text/javascript"></script>
</head>
<body>
<div id="bigBox" class="site-doc-icon site-doc-anim">
    <h1>Login</h1>
    <div class="inputBox">
        <form>
            <div class="inputText">
                <input type="text" id="userName" placeholder="请输入用户名"/>
            </div>

            <div class="inputText">
                <input type="password" id="password" placeholder="请输入密码"/>
            </div>

            <div>
                <input type="button" id="submit" value="登录" class="inputButton"/>
                <input type="button" id="register" value="注册" class="inputButton"/>
            </div>
        </form>
    </div>
</div>
<script>
     $(function (){
         $("#submit").bind("click", function (){
             login();
         })
     })
     function login() {
         //获取页面输入框的值，包装到 user js 对象中
         const user = {};
         user.userName = $("#userName").val();
         user.password = $("#password").val();
         if (user.userName == "" || user.password == ""){
             alert("请输入用户名和密码！");
             return;
         }
         //调用 ajax 请求后台的 login 控制器， 处理响应结果
         $.ajax({
             url:"/api/login",
             type:"post",
             contentType:"application/json",
             data:JSON.stringify(user),
             success:function (result){
                 if(result.status == 200){
                     window.location.href = "/dashboard";
                 }else{
                     alert(result.message);
                 }
             },
             error:function(data){
                 alert(data);
             }
         });
     }
     $(function (){
         $("#register").bind("click", function (){
             jumpRegister();
         })
     })
     function jumpRegister(){
             window.location.href = "/register"
         }
 </script>
</body>
</html>
